package com.zhouyu.service;

import org.springframework.beans.BeansException;
import org.springframework.beans.PropertyValues;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.support.MergedBeanDefinitionPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

//@Component
public class MyBeanPostProcessor implements InstantiationAwareBeanPostProcessor,MergedBeanDefinitionPostProcessor,
		InitializingBean {

	@Override
	public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
		System.out.println("执行 InstantiationAwareBeanPostProcessor.postProcessBeforeInstantiation " + beanName);
		return InstantiationAwareBeanPostProcessor.super.postProcessBeforeInstantiation(beanClass, beanName);
	}

	@Override
	public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException {
		System.out.println("执行 InstantiationAwareBeanPostProcessor.postProcessAfterInstantiation " + beanName);
		return InstantiationAwareBeanPostProcessor.super.postProcessAfterInstantiation(bean, beanName);
	}

	@Override
	public PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) throws BeansException {
		System.out.println("执行 InstantiationAwareBeanPostProcessor.postProcessProperties " + beanName);
		return InstantiationAwareBeanPostProcessor.super.postProcessProperties(pvs, bean, beanName);
	}

	@Override
	public void postProcessMergedBeanDefinition(RootBeanDefinition beanDefinition, Class<?> beanType, String beanName) {
		if(beanName.equals("userService")) {
			//beanDefinition.setInitMethodName("xxx");
			beanDefinition.setDestroyMethodName("(inferred)");
		}
		System.out.println("执行 MergedBeanDefinitionPostProcessor.postProcessMergedBeanDefinition " + beanName);
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("执行 BeanPostProcessor.postProcessAfterInitialization " + beanName);
		return bean;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		System.out.println("执行 BeanPostProcessor.postProcessBeforeInitialization " + beanName);
		return bean;
	}

	@Override
	public void afterPropertiesSet() throws Exception {

	}
}
